package com.ztp.pc.gui.ftpclients;

import java.io.IOException;

import javax.swing.table.AbstractTableModel;

import com.ztp.lib.client.Client;
import com.ztp.lib.client.SQLiteFtpClientsManager;

public class FtpClientsTableModel extends AbstractTableModel {

	
	private static final long serialVersionUID = -6910091703335675070L;

	final static String [] colNames = new String[]{
		"Id", "Host Name", "Port", "User Name"
	};
	
	private SQLiteFtpClientsManager m;
	Client [] clients;
	
	public FtpClientsTableModel(SQLiteFtpClientsManager m) throws IOException{
		this.m = m;
		clients = m.getAllClients();
	}
	
	public void refreshData() throws IOException{
		clients = m.getAllClients();
		fireTableDataChanged();
	}
	
	@Override
	public int getColumnCount() {
		return colNames.length;
	}

	@Override
	public int getRowCount() {
		return clients.length;
	}

	@Override
	public Object getValueAt(int row, int col) {
		switch (col) {
			case 0:	return clients[row].getId();
			case 1:	return clients[row].getHostName();
			case 2:	return clients[row].getPort();
			case 3:	return clients[row].getUserName();
		}
		return null;
	}

	
	@Override
	public String getColumnName(int column) {
		return colNames[column];
	}
	
	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return String.class;
	}
}
